Skip to content

Method: inferObjectPropertyValues(OWLNamedIndividual, Assertion)

1: /**
2: * Copyright (C) 2016 Czech Technical University in Prague
3: *
4: * This program is free software: you can redistribute it and/or modify it under
5: * the terms of the GNU General Public License as published by the Free Software
6: * Foundation, either version 3 of the License, or (at your option) any
7: * later version.
8: *
9: * This program is distributed in the hope that it will be useful, but WITHOUT
10: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11: * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
12: * details. You should have received a copy of the GNU General Public License
13: * along with this program. If not, see <http://www.gnu.org/licenses/>.
14: */
15: package cz.cvut.kbss.ontodriver.owlapi;
16:
17: import cz.cvut.kbss.ontodriver.owlapi.connector.OntologySnapshot;
18: import cz.cvut.kbss.ontodriver.owlapi.exception.ReasonerNotAvailableException;
19: import cz.cvut.kbss.ontodriver.owlapi.util.OwlapiUtils;
20: import cz.cvut.kbss.ontodriver.model.*;
21: import org.semanticweb.owlapi.model.*;
22: import org.semanticweb.owlapi.reasoner.OWLReasoner;
23:
24: import java.util.*;
25: import java.util.stream.Collectors;
26:
27: public class InferredAxiomLoader implements AxiomLoader {
28:
29: private final OWLReasoner reasoner;
30: private final OWLOntology ontology;
31: private final OWLDataFactory dataFactory;
32:
33: private final OwlapiAdapter adapter;
34: private final AxiomAdapter axiomAdapter;
35:
36: private NamedResource subject;
37:
38: InferredAxiomLoader(OwlapiAdapter adapter, OntologySnapshot snapshot) {
39: this.adapter = adapter;
40: this.reasoner = snapshot.getReasoner();
41: this.ontology = snapshot.getOntology();
42: this.dataFactory = snapshot.getDataFactory();
43: this.axiomAdapter = new AxiomAdapter(snapshot.getDataFactory(), adapter.getLanguage());
44: }
45:
46: @Override
47: public Collection<Axiom<?>> loadAxioms(NamedResource subject, Set<Assertion> assertions) {
48: this.subject = subject;
49: if (assertions.isEmpty()) {
50: return Collections.emptySet();
51: }
52: if (reasoner == null) {
53: throw new ReasonerNotAvailableException();
54: }
55: reasoner.flush();
56: final OWLNamedIndividual individual = OwlapiUtils.getIndividual(subject, dataFactory);
57: final Collection<Axiom<?>> axioms = new HashSet<>();
58: for (Assertion a : assertions) {
59: switch (a.getType()) {
60: case CLASS:
61: axioms.addAll(adapter.getTypesHandler().getTypes(subject, null, true));
62: break;
63: case DATA_PROPERTY:
64: axioms.addAll(inferDataPropertyValues(individual, a));
65: break;
66: case OBJECT_PROPERTY:
67: axioms.addAll(inferObjectPropertyValues(individual, a));
68: break;
69: case PROPERTY:
70: // When we don't know, try all
71: axioms.addAll(adapter.getTypesHandler().getTypes(subject, null, true));
72: axioms.addAll(inferDataPropertyValues(individual, a));
73: axioms.addAll(inferObjectPropertyValues(individual, a));
74: break;
75: default:
76: break;
77: }
78: }
79: return axioms;
80: }
81:
82: private Collection<Axiom<?>> inferDataPropertyValues(OWLNamedIndividual individual, Assertion dpAssertion) {
83: final Set<OWLLiteral> literals = reasoner.getDataPropertyValues(individual, dataProperty(dpAssertion));
84: return literals.stream().map(owlLiteral -> new AxiomImpl<>(subject, dpAssertion,
85: new Value<>(OwlapiUtils.owlLiteralToValue(owlLiteral)))).collect(Collectors.toSet());
86: }
87:
88: private OWLDataProperty dataProperty(Assertion dataPropertyAssertion) {
89: return dataFactory.getOWLDataProperty(IRI.create(dataPropertyAssertion.getIdentifier()));
90: }
91:
92: private Collection<Axiom<?>> inferObjectPropertyValues(OWLNamedIndividual individual, Assertion opAssertion) {
93: final Set<OWLNamedIndividual> individuals = reasoner.getObjectPropertyValues(individual,
94: objectProperty(opAssertion)).getFlattened();
95: return individuals.stream().map(
96: target -> axiomAdapter.createAxiom(subject, opAssertion, NamedResource.create(target.getIRI().toURI())))
97: .collect(
98: Collectors.toList());
99: }
100:
101: private OWLObjectProperty objectProperty(Assertion objectPropertyAssertion) {
102: return dataFactory.getOWLObjectProperty(IRI.create(objectPropertyAssertion.getIdentifier()));
103: }
104:
105: @Override
106: public Collection<Axiom<?>> loadPropertyAxioms(NamedResource subject) {
107: final Collection<Axiom<?>> axioms = new ArrayList<>();
108: final OWLNamedIndividual individual = OwlapiUtils.getIndividual(subject, dataFactory);
109: for (OWLDataProperty dp : ontology.getDataPropertiesInSignature()) {
110: final Set<OWLLiteral> values = reasoner.getDataPropertyValues(individual, dp);
111: for (OWLLiteral literal : values) {
112: axioms.add(axiomAdapter.createAxiom(subject,
113: Assertion.createDataPropertyAssertion(dp.getIRI().toURI(), true), literal));
114: }
115: }
116: for (OWLObjectProperty op : ontology.getObjectPropertiesInSignature()) {
117: final Set<OWLNamedIndividual> values = reasoner.getObjectPropertyValues(individual, op).getFlattened();
118: for (OWLNamedIndividual ind : values) {
119: axioms.add(axiomAdapter.createAxiom(subject,
120: Assertion.createObjectPropertyAssertion(op.getIRI().toURI(), true), NamedResource.create(
121: ind.getIRI().toURI())));
122: }
123: }
124: return axioms;
125: }
126: }